import 'dart:math';
import 'package:flutter/material.dart';

import 'GradientCircularProgressIndicator.dart';
import 'TurnBox.dart';

class GradientCircularProgressRoute extends StatefulWidget {
  const GradientCircularProgressRoute({super.key});

  @override
  _GradientCircularProgressRouteState createState() =>
      _GradientCircularProgressRouteState();
}

class _GradientCircularProgressRouteState
    extends State<GradientCircularProgressRoute>
    with TickerProviderStateMixin {
  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 3),
    )..addStatusListener(_handleAnimationStatus);
    _animationController.forward();
  }

  // 简化动画状态监听逻辑
  void _handleAnimationStatus(AnimationStatus status) {
    if (status == AnimationStatus.completed) {
      _animationController.reverse();
    } else if (status == AnimationStatus.dismissed) {
      _animationController.forward();
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  // 提取通用的进度指示器构建方法
  Widget _buildProgressIndicator({
    required List<Color> colors,
    required double radius,
    required double strokeWidth,
    bool strokeCapRound = false,
    Color backgroundColor = Colors.transparent,
    double totalAngle = 2 * pi,
    required Animation<double> animation,
  }) {
    return GradientCircularProgressIndicator(
      colors: colors,
      radius: radius,
      strokeWidth: strokeWidth,
      strokeCapRound: strokeCapRound,
      backgroundColor: backgroundColor,
      totalAngle: totalAngle,
      value: animation.value,
      stops: [],
    );
  }

  // 封装剪裁半圆的布局
  Widget _buildClippedHalfCircle() {
    return ClipRect(
      child: Align(
        alignment: Alignment.topCenter,
        heightFactor: 0.5,
        child: Padding(
          padding: const EdgeInsets.only(bottom: 8.0),
          child: SizedBox(
            child: TurnBox(
              turns: 0.75,
              child: _buildProgressIndicator(
                colors: [Colors.teal, Colors.cyan[500] ?? Colors.teal],
                radius: 100.0,
                strokeWidth: 8.0,
                strokeCapRound: true,
                totalAngle: pi,
                animation: _animationController,
              ),
            ),
          ),
        ),
      ),
    );
  }

  // 封装带文字的堆叠布局
  Widget _buildStackWithText() {
    return SizedBox(
      height: 104.0,
      width: 200.0,
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
            height: 200.0,
            top: 0.0,
            child: TurnBox(
              turns: 0.75,
              child: _buildProgressIndicator(
                colors: [Colors.teal, Colors.cyan[500] ?? Colors.teal],
                radius: 100.0,
                strokeWidth: 8.0,
                strokeCapRound: true,
                totalAngle: pi,
                animation: _animationController,
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 10.0),
            child: Text(
              "${(_animationController.value * 100).toInt()}%",
              style: TextStyle(fontSize: 25.0, color: Colors.blueGrey),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("自定义圆形进度指示")),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              AnimatedBuilder(
                animation: _animationController,
                builder: (context, child) {
                  return Padding(
                    padding: const EdgeInsets.symmetric(vertical: 16.0),
                    child: Column(
                      children: <Widget>[
                        Wrap(
                          spacing: 10.0,
                          runSpacing: 16.0,
                          children: <Widget>[
                            _buildProgressIndicator(
                              colors: [Colors.blue, Colors.blue],
                              radius: 50.0,
                              strokeWidth: 3.0,
                              animation: _animationController,
                            ),
                            _buildProgressIndicator(
                              colors: [Colors.red, Colors.orange],
                              radius: 50.0,
                              strokeWidth: 3.0,
                              animation: _animationController,
                            ),
                            _buildProgressIndicator(
                              colors: [Colors.red, Colors.orange, Colors.red],
                              radius: 50.0,
                              strokeWidth: 5.0,
                              animation: _animationController,
                            ),
                            _buildProgressIndicator(
                              colors: [Colors.teal, Colors.cyan],
                              radius: 50.0,
                              strokeWidth: 5.0,
                              strokeCapRound: true,
                              animation: CurvedAnimation(
                                parent: _animationController,
                                curve: Curves.decelerate,
                              ),
                            ),
                            TurnBox(
                              turns: 1 / 8,
                              child: _buildProgressIndicator(
                                colors: [Colors.red, Colors.orange, Colors.red],
                                radius: 50.0,
                                strokeWidth: 5.0,
                                strokeCapRound: true,
                                backgroundColor:
                                    Colors.red[50] ?? Colors.transparent,
                                totalAngle: 1.5 * pi,
                                animation: CurvedAnimation(
                                  parent: _animationController,
                                  curve: Curves.ease,
                                ),
                              ),
                            ),
                            RotatedBox(
                              quarterTurns: 1,
                              child: _buildProgressIndicator(
                                colors: [
                                  Colors.blue[700] ?? Colors.blue,
                                  Colors.blue[200] ?? Colors.blue,
                                ],
                                radius: 50.0,
                                strokeWidth: 3.0,
                                strokeCapRound: true,
                                backgroundColor: Colors.transparent,
                                animation: _animationController,
                              ),
                            ),
                            _buildProgressIndicator(
                              colors: [
                                Colors.red,
                                Colors.amber,
                                Colors.cyan,
                                Colors.green[200] ?? Colors.green,
                                Colors.blue,
                                Colors.red,
                              ],
                              radius: 50.0,
                              strokeWidth: 5.0,
                              strokeCapRound: true,
                              animation: _animationController,
                            ),
                          ],
                        ),
                        _buildProgressIndicator(
                          colors: [
                            Colors.blue[700] ?? Colors.blue,
                            Colors.blue[200] ?? Colors.blue,
                          ],
                          radius: 100.0,
                          strokeWidth: 20.0,
                          animation: _animationController,
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 16.0),
                          child: _buildProgressIndicator(
                            colors: [Colors.blue[700]!, Colors.blue[300]!],
                            radius: 100.0,
                            strokeWidth: 20.0,
                            strokeCapRound: true,
                            animation: _animationController,
                          ),
                        ),
                        _buildClippedHalfCircle(),
                        _buildStackWithText(),
                      ],
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
